package Concurrency2.Chapter01_Base;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class T01_Runnable {
    public static int max_turn = 5;
    public static class SThread extends Thread{
        private static int goodsamount = max_turn;
        @Override
        public void run() {
            for (int i = 0; i < max_turn; i++) {
                System.out.println(getName()+" -- "+(--goodsamount));
            }
        }
    }

    public static class SRunnable implements Runnable{
        private AtomicInteger goodsamount = new AtomicInteger(max_turn);
        @Override
        public void run() {
            for (int i = 0; i < max_turn; i++) {
                System.out.println(Thread.currentThread().getName()+" -- "+goodsamount.decrementAndGet());
            }
        }
    }

    public static void main(String[] args) {
        // create SThread
        for (int i = 0; i < 2; i++) {
            SThread sThread = new SThread();
            sThread.setName("Thread"+i);
            sThread.start();
        }

        System.out.println("-----------------------------");
        // create SRunnable
        for (int i = 0; i < 2; i++) {
            Thread thread = new Thread(new SRunnable());
            thread.setName("Runnable"+i);
            thread.start();
        }
    }
}
